package com.boot.mgt.service.impl

import com.boot.mgt.dao.mysql.RegionGlbmMapper
import com.boot.mgt.service.RegionGlbmService
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat

@AutoJsonRpcServiceImpl
@Service
class RegionGlbmServiceImpl: RegionGlbmService {

    @Autowired
    private lateinit var regionGlbmMapper: RegionGlbmMapper

    override fun searchRegionGlbm(map: Map<String, Any?>): List<Map<String, Any?>?>? {
        try {
            return regionGlbmMapper.searchRegionGlbm(map)
        } catch (e: Exception) {
            log.info("searchRegionGlbm: {}", e);
        }
        return emptyList()
    }

    override fun consRegion(regionId: String): String? {
        val regionLevel: String? = regionGlbmMapper.selectByPrimaryKey(regionId).level
        var regionSqlVal = if (regionLevel == "1") {
            regionId.substring(0, 2) + "%"
        } else if (regionLevel == "2") {
            if ("6113" == regionId.substring(0, 4)) {
                regionId.substring(0, 6) + "%"
            } else if ("6115" == regionId.substring(0, 4)) {
                regionId.substring(0, 4) + "%"
            } else {
                regionId.substring(0, 4) + "%"
            }
        } else {
            if ("6115" == regionId.substring(0, 10)) {
                regionId.substring(0, 10) + "%"
            } else {
                regionId.substring(0, 8) + "%"
            }
        }
        return regionSqlVal
    }

    companion object {
        private val log: Logger = LoggerFactory.getLogger(this::class.java)
        private val FORMAT = SimpleDateFormat("yyyy-MM-dd")
    }

}